package com.ptswitch.material.pages.contract;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.ptswitch.material.model.base.Upload;
import com.ptswitch.material.model.contract.Contract;
import com.ptswitch.material.pages.base.PageBase;
import com.ptswitch.material.service.ContractExcelService;
import com.ptswitch.material.service.ContractService;
import com.ptswitch.material.util.WebUtil;
import com.ptswitch.material.util.security.PermissionUtil;

public class ExportContract extends PageBase {
    @Inject
    private ContractService contractSer;
    @Inject
    private ContractExcelService excelSer;

    @OnEvent(value = EventConstants.ACTIVATE)
    public Object exportContract(long id) {
        checkPermitted(PermissionUtil.ContractExport);
        Contract contract = contractSer.findById(id);
        Upload upload = new Upload();
        upload.setFilePath("output/contract.xls");
        upload.setFileName("contract-" + contract.getCode() + ".xls");
        upload.setFileType("application/vnd.ms-excel");
        excelSer.outputContract(WebUtil.getRealFilePath(upload), contract);
        return WebUtil.download(upload);
    }
}
